package com.joesem.sale.client.logic.workOrder.viewModel

import android.app.Application
import android.graphics.Bitmap
import com.blankj.utilcode.util.ImageUtils
import com.joesem.sale.client.base.BaseViewModel
import com.joesem.sale.client.base.request
import com.joesem.sale.client.base.toast
import com.joesem.sale.client.network.data.ReceiptMode
import com.joesem.sale.client.network.data.UploadImgMode
import com.joesem.sale.client.utils.saveFile
import java.io.File


class OrderCompleteViewModel(application: Application) : BaseViewModel(application) {

    fun saveSignature(bitmap: Bitmap) {
        val file = saveFile("signature")
        val saveState = ImageUtils.save(bitmap, file, Bitmap.CompressFormat.JPEG)
        if (saveState)
            uploadFile(file, true)
    }

    fun uploadFile(file: File, isSignature: Boolean = false) {
        request(isLoading = true, request = {
            uploadImg(file)
        }, responseFailed = {
            toast(it.message)
        }, response = { it ->
            toast(it.msg)
            it.data?.let {
                if (isSignature)
                    actionIntent(SignatureUploadIntent(it))
                else {
                    actionIntent(ImgUploadIntent(it))
                }
            } ?: toast("上传失败 ${it.msg.orEmpty()}")
        })
    }

    private fun checkValue(
        values: HashMap<String, Any?>,
        service: MutableList<ReceiptMode.ServiceMode>
    ): Boolean {
        if (values["reason"] == null) {
            toast("请选择事故原因！")
            return false
        }
        if (values["process_type"] == null) {
            toast("请选择处理方式！")
            return false
        }
        if (values["description"] == null || values["description"]?.toString().isNullOrEmpty()) {
            toast("请输入详细内容！")
            return false
        }
        val images = values["images"]
        if (images == null || ((images is ArrayList<*>) && images.isNullOrEmpty())) {
            toast("请添加现场照片！")
            return false
        }
        if (service.isEmpty()) {
            toast("请添加服务项目！")
            return false
        }
        if (service.indexOfFirst { it.count.isNullOrEmpty() || it.unit.isNullOrEmpty() || it.cost.isNullOrEmpty() || it.name.isNullOrEmpty() } > -1) {
            toast("请添加服务项目填写完整！")
            return false
        }
        if (values["satisfaction"] == null || values["satisfaction"]?.toString().isNullOrEmpty()) {
            toast("请选择客户满意度！")
            return false
        }
        if (values["attitude"] == null || values["attitude"]?.toString().isNullOrEmpty()) {
            toast("请选择服务态度！")
            return false
        }
        if (values["efficiency"] == null || values["efficiency"]?.toString().isNullOrEmpty()) {
            toast("请选择服务效率！")
            return false
        }
        if (values["signature"] == null || values["signature"]?.toString().isNullOrEmpty()) {
            toast("请让客户确认并完成签名！")
            return false
        }
        return true
    }

    fun orderReceipt(
        id: String?,
        value: HashMap<String, Any?>,
        service: MutableList<ReceiptMode.ServiceMode>
    ) {
        if (checkValue(value, service))
            request(isLoading = true, request = {
                orderReceipt(id, value)
            }, responseFailed = {
                toast(it.message)
            }, response = {
                toast(it.msg)
                actionIntent(OrderReceiptIntent)
            })
    }

    class SignatureUploadIntent(val file: UploadImgMode)
    class ImgUploadIntent(val file: UploadImgMode)
    object OrderReceiptIntent
}